package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.bean.contact.MainType
import com.yc.yclibrary.base.YcAppCompatActivity
import com.yc.yclibx.adapter.YcAdapterHelper
import com.yc.yclibx.adapter.YcRecyclerViewAdapter
import com.yc.yclibx.file.YcImgUtils
import kotlinx.android.synthetic.main.main_activity.*
import kotlinx.android.synthetic.main.show_image2_activity.*
import kotlinx.android.synthetic.main.show_image3_activity.*
import kotlinx.android.synthetic.main.show_image_activity.*
import kotlinx.android.synthetic.main.show_image_activity.showImagePv

class ShowImageActivity : BaseActivity() {
    companion object {
        private const val KEY_TITLE = "key_title"
        private const val KEY_IMG_ID = "key_img_id"
        private const val KEY_XML_TYPE = "key_xml_type"
        private const val KEY_IMG_BG_IS_WHITE = "key_img_bg_is_white"
        @JvmStatic
        fun newInstance(
            activity: YcAppCompatActivity,
            title: String,
            imgResId: Int,
            xmlType: Int,
            imgBgIsWhite: Boolean
        ) {
            val intent = Intent(activity, ShowImageActivity::class.java)
            intent.putExtra(KEY_TITLE, title)
            intent.putExtra(KEY_IMG_ID, imgResId)
            intent.putExtra(KEY_XML_TYPE, xmlType)
            intent.putExtra(KEY_IMG_BG_IS_WHITE, imgBgIsWhite)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return when (intent.getIntExtra(KEY_XML_TYPE, 0)) {
            0 -> R.layout.show_image_activity
            1 -> R.layout.show_image2_activity
            2 -> R.layout.show_image3_activity
            else -> R.layout.show_image2_activity
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        when (intent.getIntExtra(KEY_XML_TYPE, 0)) {
            0 -> setImage12(bgCl)
            1 -> setImage12(bg2Cl)
            2 -> setImage3()
            else -> setImage12(bgCl)
        }
    }

    private fun setImage12(bg: ConstraintLayout) {
        if (intent.getBooleanExtra(KEY_IMG_BG_IS_WHITE, true)) {
            bg.setBackgroundResource(R.color.colorWhite)
        } else {
            bg.setBackgroundResource(R.drawable.bg_formula_2)
        }
        YcImgUtils.loadResIdImg(
            activity,
            intent.getIntExtra(KEY_IMG_ID, R.drawable.logo),
            showImagePv
        )
    }

    private fun setImage3() {
        val mAdapter = object : YcRecyclerViewAdapter<Int>(this, R.layout.show_image_item) {
            override fun onUpdate(helper: YcAdapterHelper, item: Int, position: Int) {
                YcImgUtils.loadResIdImg(activity, item, helper.getView(R.id.showImage3ItemIv))
//                helper.setImageResource(R.id.showImage3ItemIv, item)
            }
        }
        mAdapter.addAll(
            listOf(
                R.drawable.reference_12_1,
                R.drawable.reference_12_2,
                R.drawable.reference_12_3,
                R.drawable.reference_12_4,
                R.drawable.reference_12_5,
                R.drawable.reference_12_6,
                R.drawable.reference_12_7
            )
        )
        showImage3Rv.adapter = mAdapter
        showImage3Rv.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    }
}
